package com.atguigu.lease.common.result;

import lombok.Data;

/**
 * 全局统一返回结果类
 *  作用:  统一项目中的所有返回结果
 */
@Data
public class Result<T> {

    //返回码
    private Integer code; //200正常  500后端错误  404 资源不存在 403 访问受限 400 请求参数有问题

    //返回消息
    private String message; //文字说明

    //返回数据
    private T data;

    public Result() {
    }

    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static Result fail(int code, String message) {
         Result result=new Result();
         result.setCode(code);
         result.setMessage(message);
         return result;
    }
}
